unit uUtils;

interface

uses
  System.SysUtils,
  Winapi.ShellApi,
  Winapi.Windows;

function crOpenBrowser(const Url: string = 'www.google.com.br'): LongWord;

implementation

function crGetTempPath: string;
var
  Buff: array [0 .. 255] of WideChar;
begin
  GetTempPath(255, Buff);
  Result := Buff;
end;

function crGetDefaultBrowser: string;
var
  Path: array [0 .. 255] of char;
  Filename: PWideChar;
begin
  Filename := PWideChar(crGetTempPath + 'default.html');
  CloseHandle(CreateFile(Filename, GENERIC_WRITE, FILE_SHARE_WRITE, nil,
    CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0));
  FindExecutable(Filename, nil, Path);
  DeleteFile(Filename);
  Result := Path;
end;

function crOpenBrowser(const Url: string = 'www.google.com.br'): LongWord;
var
  Startupinfo: TStartupInfo;
  Processinfo: Tprocessinformation;
begin
  Fillchar(Startupinfo, Sizeof(Startupinfo), #0);
  Startupinfo.cb := Sizeof(Startupinfo);
  Startupinfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
  Startupinfo.wShowWindow := SW_SHOWNORMAL;

  if not Createprocess(nil, PWideChar(crGetDefaultBrowser + ' ' + Url) , nil, nil, False,
    CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, Startupinfo,
    Processinfo) then
    Result := WAIT_FAILED;

  if Processinfo.Hprocess <> 0 then
    CloseHandle(Processinfo.Hprocess);
  if Processinfo.Hthread <> 0 then
    CloseHandle(Processinfo.Hthread);
end;

end.
